package cz.data.domain.quality.controller;

import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.quality.mapstruct.CheckRuleMapper;
import cz.data.domain.quality.model.dto.CheckRuleDto;
import cz.data.domain.quality.model.entity.CheckRuleEntity;
import cz.data.domain.quality.model.query.CheckRuleQuery;
import cz.data.domain.quality.model.vo.CheckRuleVo;
import cz.data.domain.quality.service.CheckRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"核查规则"})
@RestController
@RequestMapping("/checkRules")
public class CheckRuleController extends BaseController {

    @Autowired
    private CheckRuleService checkRuleService;

    @Autowired
    private CheckRuleMapper checkRuleMapper;

    @ApiOperation(value = "核查规则-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<CheckRuleVo> getCheckRuleById(@PathVariable String id) {
        CheckRuleEntity checkRuleEntity = checkRuleService.getCheckRuleById(id);
        return new SimpleDataResponse<>(checkRuleMapper.toVO(checkRuleEntity));
    }

    @ApiOperation(value = "核查规则-分页查询")
    @ApiImplicitParam(name = "checkRuleQuery", value = "查询实体checkRuleQuery", required = true, dataTypeClass = CheckRuleQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<CheckRuleVo>> getCheckRulePage(CheckRuleQuery checkRuleQuery) {
        QueryWrapper<CheckRuleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(checkRuleQuery.getRuleTypeId()), "r.rule_type_id", checkRuleQuery.getRuleTypeId());
        queryWrapper.like(StringUtils.isNotBlank(checkRuleQuery.getRuleName()), "r.rule_name", checkRuleQuery.getRuleName());
        queryWrapper.like(StringUtils.isNotBlank(checkRuleQuery.getRuleSource()), "r.rule_source", checkRuleQuery.getRuleSource());
        queryWrapper.like(StringUtils.isNotBlank(checkRuleQuery.getRuleTable()), "r.rule_table", checkRuleQuery.getRuleTable());
        queryWrapper.like(StringUtils.isNotBlank(checkRuleQuery.getRuleColumn()), "r.rule_column", checkRuleQuery.getRuleColumn());
        IPage<CheckRuleEntity> page = checkRuleService.page(new Page<>(checkRuleQuery.getPageNum(), checkRuleQuery.getPageSize()), queryWrapper);
        List<CheckRuleVo> collect = page.getRecords().stream().map(checkRuleMapper::toVO).collect(Collectors.toList());
        JsonPage<CheckRuleVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "核查规则-新增")
    @ApiImplicitParam(name = "checkRule", value = "详细实体checkRule", required = true, dataType = "CheckRuleDto")
    @PostMapping()
    public SimpleDataResponse<CheckRuleVo> saveCheckRule(@RequestBody @Validated({ValidationGroups.Insert.class}) CheckRuleDto checkRule) {
        CheckRuleEntity checkRuleEntity = checkRuleService.saveCheckRule(checkRule);
        return new SimpleDataResponse<>(checkRuleMapper.toVO(checkRuleEntity));
    }

    @ApiOperation(value = "核查规则-修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "checkRule", value = "详细实体checkRule", required = true, dataType = "CheckRuleDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<CheckRuleVo> updateCheckRule(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) CheckRuleDto checkRule) {
        CheckRuleEntity checkRuleEntity = checkRuleService.updateCheckRule(checkRule);
        return new SimpleDataResponse<>(checkRuleMapper.toVO(checkRuleEntity));
    }

    @ApiOperation(value = "核查规则-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteCheckRuleById(@PathVariable String id) {
        checkRuleService.deleteCheckRuleById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "核查规则-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteCheckRuleBatch(@PathVariable List<String> ids) {
        checkRuleService.deleteCheckRuleBatch(ids);
        return BaseResponse.success();
    }
}
